﻿using Yxinmt.Interface;
using System;
using System.Collections.Generic;

namespace Yxinmt.Controls
{
    public class ImageSelectButton : BaseControl, IControl
    {
        System.Windows.Controls.Grid _Grid { get; set; }

        Image _Image { get; set; }
        Image _SelectedImage { get; set; }
        bool _Selected { get; set; }

        private System.Windows.Controls.Button _Button { get; set; }

        public ImageSelectButton(Dictionary<string, object> property, IComponent component)
        {
            Property = property;
            _Component = component;
            _Grid = new System.Windows.Controls.Grid();
            this.Element = _Grid;

            InitSet();
            SetControlProperty();

            InitImages();
        }

        private void InitImages()
        {
            Dictionary<string, object> property = new Dictionary<string, object>();
            property["ZIndex"] = Utility.Common.GetPanelZIndex();

            this.SetPropertyKvp(new List<string> { "name", "ImageUrl", "ZIndex" }, property);
            _Image = new Image(property, _Component, true);
            this.Add(_Image.Element);

            property = new Dictionary<string, object>();
            property["ControlAlign"] = "MiddleCenter";
            property["ZIndex"] = Utility.Common.GetPanelZIndex();
            property["Visible"] = false;

            if (Property.ContainsKey("SelectedImageUrl")) property["ImageUrl"] = Property["SelectedImageUrl"];
            if (Property.ContainsKey("SelectedImageName")) property["name"] = Property["SelectedImageName"];

            this.SetPropertyKvp(new List<string> { "name", "ImageUrl", "Visible", "ZIndex" }, property);
            _SelectedImage = new Image(property, _Component, true);
            this.Add(_SelectedImage.Element);
        }

        public override void EventLoad()
        {
            _Image.BindClick();
            _Image.ClickAction = (sender, e, ctl) => SetSelected();

            _SelectedImage.BindClick();
            _SelectedImage.ClickAction = (sender, e, ctl) => SetSelected();
        }

        public Action<IControl, bool> SelectChanged { get; set; }
        private void SetSelected()
        {
            _Selected = !_Selected;
            this._SelectedImage.SetVisible(_Selected);

            if (SelectChanged != null) SelectChanged(this, _Selected);
        }

        public override void Add(System.Windows.FrameworkElement control)
        {
            _Grid.Children.Add(control);
        }

        public override object GetValue()
        {
            return _Selected;
        }

        public override void SetValue(object value)
        {
            if (value != null && (bool)value) { _Selected = true; }
            this._SelectedImage.SetVisible(_Selected);
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.Grid c = _Grid;
            switch (name)
            {
                case "name": c.Name = string.Concat("imggrid_", (string)value); break;
                case "Height": c.Height = Convert.ToDouble(value); break;
                case "Width": c.Width = Convert.ToDouble(value); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "BackColor": c.Background = GetColor((string)value); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "Visible": SetVisible(Convert.ToBoolean(value)); break;
                case "GridRowColumn": SetGridRowColumn((string)value); break;
                case "ZIndex": SetZIndex(Convert.ToInt32(value)); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex(Convert.ToInt32(value)); break;
            }
        }
    }
}
